import App from './App'
import uView from '@/uni_modules/uview-ui'
Vue.use(uView)
// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
	...App
})
app.$mount()
// #endif

// #ifdef VUE3
import {
	createSSRApp
} from 'vue'
import {
	removeData
} from 'jquery'
export function createApp() {
	const app = createSSRApp(App)
	return {
		app
	}
}
// #endif

//创建类
const ran = {
	// location: 'https://wt.huanti3d.com', 
	// location: 'http://localhost:48888',
	// location: 'http://172.16.10.35:48888',
	location: 'https://api.huanti3d.com',
	// imgpath: 'https://test.huanti3d.com', 
	// imgpath: 'https://test.huanti3d.com',
	imgpath: 'https://api.huanti3d.com',
	// location: 'http://localhost:48888',
	// imgpath: 'https://test.huanti3d.com',
	version: '1.1.0',
	timetrans: function(date) {
		var date = new Date(date);
		date.setTime(date);
		var Y = date.getFullYear() + '-';
		var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
		var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
		var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
		var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
		var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
		return Y + M + D + h + m + s;
	},
	request: function(location, data, methods) {
		//公共参数
		var userdata = wx.getStorageSync('userdata');
		var accessToken = uni.getStorageSync('accessToken');
		var userId = uni.getStorageSync('userId');
		var api_data = {

		}
		var tmp = Object.assign(data, api_data)
		return new Promise((resolve, reject) => {
			if (methods == 'GET') {
				uni.request({
					url: this.location + location,
					data: JSON.stringify(tmp),
					dataType: 'json',
					header: {
						'tenant-id': '1', //自定义请求头信息
						'Authorization': 'Bearer ' + accessToken, //自定义请求头信息
					},
					success: (res) => {
						// console.log(res);
						var data;
						// console.log(typeof(res.data))
						if (typeof(res.data) == 'string') {
							// if(res.data.code == null){
							data = JSON.parse(res.data);
						} else {
							data = res.data;
						}
						if (data.code == 200) {
							resolve(data)
						} else {

							uni.showToast({
								title: data.msg,
								icon: 'none',
							});
							uni.removeStorage({
								key: 'accessToken'
							})
							uni.removeStorage({
								key: 'userId'
							})
							setTimeout(function() {
								uni.navigateTo({
									url: '../login/index'
								})
							}, 1500)
						}
					},
					fail: (err) => {
						uni.showToast({
							title: '网络出错',
							icon: 'error',
							duration: 2000
						});
					}
				});
			} else {
				uni.request({
					url: this.location + location,
					data: JSON.stringify(tmp),
					method: methods,
					dataType: 'json',
					header: {
						'tenant-id': '1', //自定义请求头信息
						'Authorization': 'Bearer ' + accessToken, //自定义请求头信息

					},
					success: (res) => {
						// console.log(res);
						var data;
						// console.log(typeof(res.data))
						if (typeof(res.data) == 'string') {
							// if(res.data.code == null){
							data = JSON.parse(res.data);
						} else {
							data = res.data;
						}
						if (data.code == 401) {
							uni.showToast({
								title: data.msg,
								icon: 'none',
							});
							uni.navigateTo({
								url: '../login/index'
							})
						} else {
							resolve(data)
						}
					},
					fail: (err) => {
						uni.showToast({
							title: '网络出错',
							icon: 'error',
							duration: 2000
						});
					}
				});
			}

		})
	}
}
Vue.prototype.ran = ran; //推到全局